package 力扣题库;

import java.util.Stack;

public class p155最小栈 {
    class MinStack {
        private Stack<Integer> stack;
        private Stack<Integer> minStack;

        public MinStack() {
            stack=new Stack<>();
            minStack=new Stack<>();

        }

        public void push(int val) {
            stack.push(val);
            if (minStack.isEmpty() || minStack.peek()>val) {
                minStack.push(val );
            }
        }

        public void pop() {
            //删除普通栈顶元素,注意最小栈的更新
            int pop=stack.pop();
            if (pop==minStack.peek()) {
                minStack.pop();
            }

        }

        public int top() {
            //查看普通栈顶元素
            if (stack.isEmpty()) {
                return -1;
            }
            return stack.peek();
        }

        public int getMin() {
            //通过最小栈的栈顶元素 查看当前栈中的最小元素
            if (minStack.isEmpty()) {
                return -1;
            }
            return minStack.peek();

        }
    }
}
